// Decompiled with JetBrains decompiler
// Type: MetaConstantPool
// Assembly: FMCommon, Version=1.0.8663.21262, Culture=neutral, PublicKeyToken=null
// MVID: 52773074-FB09-4BA0-8FA9-4782D11D6C17
// Assembly location: /Users/mac/work/RiverGame/com.rivergame.fmframework/Runtime/Plugins/FMCommon.dll

using FibMatrix.Config;
using System.Collections.Generic;
using System.IO;

public class MetaConstantPool
{
  private Dictionary<string, int> _stringPool = new Dictionary<string, int>();
  private Dictionary<int, int> _intPool = new Dictionary<int, int>();
  private Dictionary<long, int> _longPool = new Dictionary<long, int>();
  private Dictionary<float, int> _floatPool = new Dictionary<float, int>();
  private Dictionary<double, int> _doublePool = new Dictionary<double, int>();
  public const int POOL_MAX_SIZE = 16777215;

  public int GetStringIndex(string val, bool addNotExist = true, int maxIndex = 16777215)
  {
    int count;
    if (!this._stringPool.TryGetValue(val, out count))
    {
      count = this._stringPool.Count;
      if (count > maxIndex || !addNotExist)
        return -1;
      this._stringPool.Add(val, count);
    }
    return count;
  }

  public int GetIntIndex(int val, bool addNotExist = true, int maxIndex = 16777215)
  {
    int count;
    if (!this._intPool.TryGetValue(val, out count))
    {
      count = this._intPool.Count;
      if (count > maxIndex || !addNotExist)
        return -1;
      this._intPool.Add(val, count);
    }
    return count;
  }

  public int GetLongIndex(long val, bool addNotExist = true, int maxIndex = 16777215)
  {
    int count;
    if (!this._longPool.TryGetValue(val, out count))
    {
      count = this._longPool.Count;
      if (count > maxIndex || !addNotExist)
        return -1;
      this._longPool.Add(val, count);
    }
    return count;
  }

  public int GetFloatIndex(float val, bool addNotExist = true, int maxIndex = 16777215)
  {
    int count;
    if (!this._floatPool.TryGetValue(val, out count))
    {
      count = this._floatPool.Count;
      if (count > maxIndex || !addNotExist)
        return -1;
      this._floatPool.Add(val, count);
    }
    return count;
  }

  public int GetDoubleIndex(double val, bool addNotExist = true, int maxIndex = 16777215)
  {
    int count;
    if (!this._doublePool.TryGetValue(val, out count))
    {
      count = this._doublePool.Count;
      if (count > maxIndex || !addNotExist)
        return -1;
      this._doublePool.Add(val, count);
    }
    return count;
  }

  private static T[] GetUniqueList<T>(Dictionary<T, int> pool)
  {
    T[] objArray = new T[pool.Count];
    foreach (KeyValuePair<T, int> keyValuePair in pool)
      objArray[keyValuePair.Value] = keyValuePair.Key;
    return objArray;
  }

  public bool WriteTo(BinaryWriter writer, MetaRawContext lastMetaContext)
  {
    HashSet<string> stringSet = lastMetaContext != null ? new HashSet<string>((IEnumerable<string>) lastMetaContext.uniqueStrs) : new HashSet<string>();
    string[] uniqueList1 = MetaConstantPool.GetUniqueList<string>(this._stringPool);
    bool flag = false;
    writer.Write(uniqueList1.Length - stringSet.Count);
    foreach (string str in uniqueList1)
    {
      if (!stringSet.Contains(str))
      {
        flag = true;
        writer.Write(str);
      }
    }
    HashSet<int> intSet = lastMetaContext != null ? new HashSet<int>((IEnumerable<int>) lastMetaContext.uniqueInts) : new HashSet<int>();
    int[] uniqueList2 = MetaConstantPool.GetUniqueList<int>(this._intPool);
    writer.Write(uniqueList2.Length - intSet.Count);
    foreach (int num in uniqueList2)
    {
      if (!intSet.Contains(num))
      {
        flag = true;
        writer.Write(num);
      }
    }
    HashSet<long> longSet = lastMetaContext != null ? new HashSet<long>((IEnumerable<long>) lastMetaContext.uniqueLongs) : new HashSet<long>();
    long[] uniqueList3 = MetaConstantPool.GetUniqueList<long>(this._longPool);
    writer.Write(uniqueList3.Length - longSet.Count);
    foreach (long num in uniqueList3)
    {
      if (!longSet.Contains(num))
      {
        flag = true;
        writer.Write(num);
      }
    }
    HashSet<float> floatSet = lastMetaContext != null ? new HashSet<float>((IEnumerable<float>) lastMetaContext.uniqueFloats) : new HashSet<float>();
    float[] uniqueList4 = MetaConstantPool.GetUniqueList<float>(this._floatPool);
    writer.Write(uniqueList4.Length - floatSet.Count);
    foreach (float num in uniqueList4)
    {
      if (!floatSet.Contains(num))
      {
        flag = true;
        writer.Write(num);
      }
    }
    HashSet<double> doubleSet = lastMetaContext != null ? new HashSet<double>((IEnumerable<double>) lastMetaContext.uniqueDoubles) : new HashSet<double>();
    double[] uniqueList5 = MetaConstantPool.GetUniqueList<double>(this._doublePool);
    writer.Write(uniqueList5.Length - doubleSet.Count);
    foreach (double num in uniqueList5)
    {
      if (!doubleSet.Contains(num))
      {
        flag = true;
        writer.Write(num);
      }
    }
    return flag;
  }

  public void ReadFrom(MetaRawContext context)
  {
    if (context == null)
      return;
    foreach (string uniqueStr in context.uniqueStrs)
      this.GetStringIndex(uniqueStr, true, 16777215);
    foreach (int uniqueInt in context.uniqueInts)
      this.GetIntIndex(uniqueInt, true, 16777215);
    foreach (float uniqueFloat in context.uniqueFloats)
      this.GetFloatIndex(uniqueFloat, true, 16777215);
    foreach (double uniqueDouble in context.uniqueDoubles)
      this.GetDoubleIndex(uniqueDouble, true, 16777215);
    foreach (long uniqueLong in context.uniqueLongs)
      this.GetLongIndex(uniqueLong, true, 16777215);
  }
}
